import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { heartfire } from './heartfire';
const { ccclass, property } = _decorator;

@ccclass('heartController')
export class heartController extends Component {
    @property(Prefab) fire:Prefab;
    @property speed:number = 20;
    flag:boolean = false;
    start() {
        this.createFire();
    }
    createFire()
    {
        for(let i = -250;i<250;i+=40)
        {
            const topfire = instantiate(this.fire)
            const downfire = instantiate(this.fire)
            this.node.addChild(topfire);
            this.node.addChild(downfire);
            topfire.setPosition(i,this.getTopY(i))
            downfire.setPosition(i,this.getDownY(i))
        }
        const last = instantiate(this.fire)
        this.node.addChild(last)
        last.setPosition(250,0)
    }
    getTopY(x:number)
    {
        return 120 * Math.sqrt(1 - (0.008 * Math.abs(x) - 1) ** 2);
    }
    getDownY(x:number)
    {
        return 120 * (Math.acos(1 - 0.008 * Math.abs(x)) - Math.PI);
    }
    move(deltaTime)
    {
        const x = this.node.getPosition().x;
        const y = this.node.getPosition().y;
        if(y<155)
        {
            this.node.setPosition(x,y + this.speed * deltaTime)
        }
        else if(this.flag === false)
        {
            const chi = this.node.children
            for(let i = 0;i<chi.length;i++)
            {
                chi[i].getComponent(heartfire).die();
            }
            this.flag = true;
        }
    }
    update(deltaTime: number) {
        this.move(deltaTime);
    }
}


